package com.far.dao;
import java.io.IOException;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import oracle.jdbc.OracleTypes;

import org.apache.log4j.Logger;

import com.far.dto.RecetasDTO;
import com.fic.dto.MedicamentoDTO;

import utiles.AppProperties;
import utiles.ConexionPool;
import utiles.utilitarios;


public class FarRecetaDAO {
	private static Logger log = Logger.getLogger(FarGuiaDespachoDAO.class);
	String pkgVar = (AppProperties.getProperty("aplicacion", "PACKAGE"));
	String horaActual = utilitarios.getFechaHoraActual(); //utilitarios.getHoraActual();
	
	public List<MedicamentoDTO> getMedicamentosRecetaSM(String idPaciente) throws SQLException, IOException{
		Connection conn = ConexionPool.getConnectionPool();	
		List<MedicamentoDTO> listaMedicamentos = new ArrayList<MedicamentoDTO>();
		
		try {
			String query = "{ call "+pkgVar+".PKG_FAR_RECETA.getRecetaSM(?, ?) }"; 	       
			CallableStatement  stmt = conn.prepareCall(query);
			stmt.setString(1, idPaciente);
			stmt.registerOutParameter(2, OracleTypes.CURSOR);
			stmt.execute();
			ResultSet rs = (ResultSet)stmt.getObject(2);

			while (rs.next()) {
				MedicamentoDTO medDTO = new MedicamentoDTO();
				medDTO.setDescMedicamento(rs.getString(1));
				medDTO.setCantidad(rs.getString(2));
				medDTO.setVia(rs.getString(3));
				medDTO.setDosis(rs.getString(4));
				medDTO.setTiempo(rs.getString(5));
				medDTO.setFechaDigitacion(rs.getString(6));
				listaMedicamentos.add(medDTO);
			}
			rs.close();
			stmt.close();
		} catch (SQLException ex) {  
			log.error("Error FarRecetaDAO.getMedicamentosRecetaSM SQLException: " +ex.getMessage());
			System.out.println(ex.getMessage());
		} finally {
			//ConexionPool.desconectaPool(conn);
			conn.close();
			conn = null;
		}
		return listaMedicamentos;

	}
	
	public List<RecetasDTO> getRecetaRCC(String serie, String numero ) throws SQLException, IOException{
		Connection conn = ConexionPool.getConnectionPool();	
		List<RecetasDTO> listaReceta = new ArrayList<RecetasDTO>();
		
		try {
			String query = "{ call "+pkgVar+".PKG_FAR_RECETA.getRecetaRCC(?, ?, ?) }"; 	       
			CallableStatement  stmt = conn.prepareCall(query);
			stmt.setString(1, serie);
			stmt.setString(2, numero);
			stmt.registerOutParameter(3, OracleTypes.CURSOR);
			stmt.execute();
			ResultSet rs = (ResultSet)stmt.getObject(3);

			while (rs.next()) {
				RecetasDTO recDTO = new RecetasDTO();
				recDTO.setIdSuc(rs.getString(1));
				recDTO.setFemi(rs.getString(2));
				recDTO.setRutm(rs.getString(3));
				recDTO.setDvm(rs.getString(4));
				recDTO.setNomMedico(rs.getString(5));
				recDTO.setRutp(rs.getString(6));
				recDTO.setDvp(rs.getString(7));
				recDTO.setNomPac(rs.getString(8));
				recDTO.setRutr(rs.getString(9));
				recDTO.setDvr(rs.getString(10));
				recDTO.setNomRet(rs.getString(11));
				recDTO.setIdInf(rs.getString(12));
				recDTO.setDosi(rs.getString(13));
				recDTO.setTotD(rs.getString(14));
				recDTO.setCanP(rs.getString(15));
				recDTO.setEstado(rs.getString(16));
				recDTO.setFdesp(rs.getString(17));
				listaReceta.add(recDTO);
			}
			rs.close();
			stmt.close();
		} catch (SQLException ex) {  
			log.error("Error FarRecetaDAO.getRecetaRCC SQLException: " +ex.getMessage());
			System.out.println(ex.getMessage());
		} finally {
			//ConexionPool.desconectaPool(conn);
			conn.close();
			conn = null;
		}
		return listaReceta;

	}
	
	
	public  int setActualizaRCC(String serie, String numero, String rutp, String rutm, String femi, String dosis, String totald, String cantidap, String rutr, String nomr ) throws SQLException, IOException{

		Connection conn = ConexionPool.getConnectionPool();	
    	int retorno=0;	    	
		try {
			//System.out.println("se envia serie:"+serie +" / numero:"+numero+" /rutp:"+rutp+" /rutm:"+rutm+" /femi:"+femi);
			String query = "{ call "+pkgVar+".Pkg_far_receta.setActualizaRCC(?,?,?,?,?,?,?,?,?,?) }"; 	       
			CallableStatement  stmt = conn.prepareCall(query);
			stmt.setString(1, serie);
			stmt.setString(2, numero);
			stmt.setString(3, rutp);
			stmt.setString(4, rutm);
			stmt.setString(5, femi);
			stmt.setString(6, dosis);
			stmt.setString(7, totald);
			stmt.setString(8, cantidap);
			stmt.setString(9, rutr);
			stmt.setString(10, nomr);
			stmt.execute();

			stmt.close();
			retorno=1;
		} catch (SQLException ex) {  
			log.error("Error FarRecetaDAO.setActualizaRCC SQLException: " +ex.getMessage());
			System.out.println(ex.getMessage());
		} finally {
			conn.close();			

            conn = null;
		}
		return retorno;	
	}
	
	
	
	public String setValidaRx(String serie, String numero, String receta ) throws SQLException, IOException{

		Connection conn = ConexionPool.getConnectionPool();	
    	String retorno="";	    	
		try {
			//System.out.println("se envia serie:"+serie +" / numero:"+numero+" /rutp:"+rutp+" /rutm:"+rutm+" /femi:"+femi);
			String query = "{ call "+pkgVar+".Pkg_far_receta.getValidaByReceta(?,?,?,?) }"; 	       
			CallableStatement  stmt = conn.prepareCall(query);
			stmt.setString(1, serie);
			stmt.setString(2, numero);
			stmt.setString(3, receta);
			stmt.registerOutParameter(4, OracleTypes.VARCHAR);
			stmt.execute();
			retorno=stmt.getString(4);
			stmt.close();
			
		} catch (SQLException ex) {  
			log.error("Error FarRecetaDAO.getValidaRx SQLException: " +ex.getMessage());
			System.out.println(ex.getMessage());
		} finally {
			conn.close();			

            conn = null;
		}
		return retorno;	
	}
	
	
	
	
	
	
	
}
